#!/usr/bin/env ruby
def lock2key (lock) #WHICH IDIOT DEVELOP IT?
	key=String.new(lock.to_s)
	#calculating
	key[0] = lock[0] ^ lock[-1] ^ lock[-2] ^ 5
	for i in 1..lock.length-1 do
	
		key[i] = lock[i] ^ lock[i-1]
	end
	#swapping
	for i in 0..lock.length-1 do
		key[i]=(key[i]<<4 & 240) |((key[i]>>4) & 15)
	end

	#fixing chars
	rchars={0=>"000", 5=>"005", 36=>"036", 96=>"096", 124=>"124", 126=>"126"}
	tmp=" "
	rchars.each {|keyr, value|
		tmp[0]=keyr
		key=key.gsub(tmp,"/%DCN"+value+"%/")
	}
	
	
	return key
end

class DCUser
	def initialize(params_or_myinfo=nil)
		pom = (params_or_myinfo==nil) ? Hash.new : params_or_myinfo #just a shortcut
		if pom.class==String
			ar=pom.split("$")
			ar2=ar[2].split(" ",3)
			@nick=ar2[1]
			@description=ar2[2].match(/.*?(?= <)/)
			@tag=ar2[2].match(/<.*?>/).to_s
			@connection=ar[4][0...ar[4].length-1]
			@flag=("" << ar[4][-1])
			@mail=ar[5]
			@share=ar[6]
		else
			@nick=pom.has_key?(:nick) ? pom[:nick] : "WHEREISMYNICK"
			@description=pom.has_key?(:description) ? pom[:description] : "RubyDC++"
			@tag=pom.has_key?(:tag) ? pom[:tag] : "<RubyDC V:r28,M:P,H:0/1/0,S:2>"
			@connection=pom.has_key?(:connection) ? pom[:connection] : "LAN(T1)"
			@flag=pom.has_key?(:flag) ? pom[:flag] : 1
			@mail=pom.has_key?(:mail) ? pom[:mail] : ""
			@share=pom.has_key?(:share) ? pom[:share] : 0
		end
	end
	
	def myInfo
		return "$MyINFO $ALL #{@nick} #{@description} #{@tag}$ $#{@connection}#{@flag}$#{@mail}$#{@share}$"

	end

	attr_accessor :nick
	attr_accessor :description
	attr_accessor :connection
	attr_accessor :flag
	attr_accessor :mail
	attr_accessor :share
end
if __FILE__ == $0
	a=DCUser.new
	b=a.myInfo
	c=DCUser.new(b)
	puts a.myInfo
	puts c.myInfo
end

